﻿using System;
using System.Collections.Generic;
using System.Text;

namespace OkTool.Web
{
    public class R<T>
    {
        /// <summary>
        /// 成功
        /// </summary>
        public static int SUCCESS = 200;

        /// <summary>
        /// 失败
        /// </summary>
        public static int FAIL = 500;

        /// <summary>
        /// 
        /// </summary>
        public int Code { get; set; } = 200;

        /// <summary>
        /// 错误消息
        /// </summary>
        public string Msg { get; set; }

        /// <summary>
        /// 执行返回的数据
        /// </summary>
        public T Data { get; set; }

        /// <summary>
        /// 成功
        /// </summary>
        /// <returns></returns>
        public static R<T> Ok()
        {
            return restResut(default, SUCCESS, null);
        }

        /// <summary>
        /// 成功
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static R<T> Ok(T data)
        {
            return restResut(data, SUCCESS, null);
        }

        /// <summary>
        /// 成功
        /// </summary>
        /// <param name="data"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static R<T> Ok(T data, string msg)
        {
            return restResut(data, SUCCESS, msg);
        }

        /// <summary>
        /// 失败
        /// </summary>
        /// <returns></returns>
        public static R<T> Fail()
        {
            return restResut(default, FAIL, null);
        }

        /// <summary>
        /// 失败
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static R<T> Fail(string msg)
        {
            return restResut(default, FAIL, msg);
        }

        /// <summary>
        /// 失败
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static R<T> Fail(T data)
        {
            return restResut(data, FAIL, null);
        }

        /// <summary>
        /// 失败
        /// </summary>
        /// <param name="data"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static R<T> Fail(T data, string msg)
        {
            return restResut(data, FAIL, msg);
        }

        /// <summary>
        /// 失败
        /// </summary>
        /// <param name="code"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static R<T> Fail(int code, string msg)
        {
            return restResut(default, code, msg);
        }
        private static R<T> restResut(T data, int code, string msg)
        {
            R<T> apiResult = new R<T>();
            apiResult.Code = code;
            apiResult.Data = data;
            apiResult.Msg = msg;
            return apiResult;
        }

    }
    public class R
    {
        /// <summary>
        /// 成功
        /// </summary>
        /// <returns></returns>
        public static R<object> Ok()
        {
            return R<object>.Ok();
        }

        /// <summary>
        /// 成功
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static R<object> Ok(object data)
        {
            return R<object>.Ok(data);
        }

        /// <summary>
        /// 成功
        /// </summary>
        /// <param name="data"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static R<object> Ok(object data, string msg)
        {
            return R<object>.Ok(data, msg);
        }

        /// <summary>
        /// 失败
        /// </summary>
        /// <returns></returns>
        public static R<object> Fail()
        {
            return R<object>.Fail();
        }

        /// <summary>
        /// 失败
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static R<object> Fail(string msg)
        {
            return R<object>.Fail(msg);
        }

        /// <summary>
        /// 失败
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static R<object> Fail(object data)
        {
            return R<object>.Fail(data);
        }

        /// <summary>
        /// 失败
        /// </summary>
        /// <param name="data"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static R<object> Fail(object data, string msg)
        {
            return R<object>.Fail(data, msg);
        }

        /// <summary>
        /// 失败
        /// </summary>
        /// <param name="code"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static R<object> Fail(int code, string msg)
        {
            return R<object>.Fail(code, msg);
        }
    }
}
